package com.teamF.mockdata;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class getParaList {

    public static List<String> doGet() {
        List list = null;
        Connection conn = getConn();
        list = getAll(conn);
        return list;
    }


    private static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://112.74.40.18:3306/bridge?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    private static List getAll(Connection connection) {
        Connection conn = connection;
        String sql = "select * from sensor";
        PreparedStatement pstmt;
        List<String> list = new ArrayList();
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            //System.out.println("============================");

            while (rs.next()) {
                if(!list.contains(rs.getString(4))){
                    list.add(rs.getString(4));
                }
                //System.out.println("");
            }
            //System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

